package analyzemytime.anlyzemytime;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import analyzemytime.datamodel.Employee;
import analyzemytime.datamodel.Project;
import analyzemytime.datamodel.Report;
import analyzemytime.reports.MonthlyEmployeeReport;

public class ReportAggregator {
	String path = "resources\\";
	private String month;
	private String year;
	
	public ReportAggregator(String month,String year){
		this.month = month;
		this.year = year;
	}
	
	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public ArrayList<Report> getMonthlyReport(){
	  XMLReader xmlReader = new XMLReader();
	  ArrayList<Report> reports = new ArrayList<Report>();
	  String myDirectoryPath = path+this.getYear()+"\\"+this.getMonth();
	  File dir = new File(myDirectoryPath);
	  File[] directoryListing = dir.listFiles();
	  if (directoryListing != null) {
	    for (File child : directoryListing) {
	    	reports.addAll(xmlReader.readXLSFile(child.getAbsolutePath()));
	    }
	  } else {

	  }
	  return reports;
	}
	
	public HashMap<Employee, ArrayList<Report>> segregateByEmployee(ArrayList<Report> reports){
		HashMap<Employee, ArrayList<Report>> result = new HashMap<Employee, ArrayList<Report>>();
		for(Report report : reports){
			if(!result.containsKey(report.getEmployee())){
				ArrayList<Report> list = new ArrayList<Report>();
				list.add(report);
				result.put(report.getEmployee(), list);
			}else{
				ArrayList<Report> list = result.get(report.getEmployee());
				list.add(report);
				result.put(report.getEmployee(), list);
			}
		}
		return result; 
	}
	
	public HashMap<Project, ArrayList<Report>> segregateByProject(ArrayList<Report> reports){
		HashMap<Project, ArrayList<Report>> result = new HashMap<Project, ArrayList<Report>>();
		for(Report report : reports){
			if(!result.containsKey(report.getProject())){
				ArrayList<Report> list = new ArrayList<Report>();
				list.add(report);
				result.put(report.getProject(), list);
			}else{
				ArrayList<Report> list = result.get(report.getProject());
				list.add(report);
				result.put(report.getProject(), list);
			}
		}
		return result; 
	}

}
